<script type="text/javascript">
	$(document).ready(function(){
		

		var form_action = "<?php echo $action; ?>";

		
		if(form_action == "add"){
			$("#tournament_id").val("<?php echo $tournamentid; ?>");
			
		}else if(form_action == "edit"){
			//$("#name_label,#country_label").addClass("required");
			
			$("#tournament_id").val("<?php echo isset($tournament)?$tournament[0]->tournamentid:""; ?>");
			$("#tournament_name").val("<?php echo isset($tournament)?$tournament[0]->tour_name:""; ?>");
			$("#start_date").val("<?php echo isset($tournament)?$tournament[0]->start_date:""; ?>");
			$("#end_date").val("<?php echo isset($tournament)?$tournament[0]->end_date:""; ?>");

			$enabled = "<?php echo isset($tournament)?$tournament[0]->enabled:""; ?>";
			if($enabled == "1"){
				$("#tournament_enabled").attr("checked","checked");				
			}else{
				$("#tournament_enabled").removeAttr("checked");
			}
			
		}else{
			alert("Operation Invalid.");
		}

		$("#save").on('click', function (event) {
			var url = "<?php echo base_url(); ?>index.php/tournament/";

			if (form_action == "add"){
				url += "doCreateGame";
			}else if(form_action == "edit"){
				url += "doEditGame";
			}

			 var form_data = $('form').serialize();
			
			var request = $.ajax({
				url: url,
				type: "POST",			
			    async: false,
		        dataType: "html",
		        data: form_data
			});

			request.done(function(msg) {				
				if (form_action == "add"){
					
								
				}
					
				alert("Data Saved");		
			});

			request.fail(function(jqXHR, textStatus) {
				alert( "Request failed: " + textStatus );
			});
		});
		

		$("#back").on('click', function (event) {
			window.location = "<?php echo base_url(); ?>index.php/tournament";
		});

		 $( "#start_date" ).datepicker({
		     showOn: "button",
             //buttonImage: "/Content/images/calendar.gif",
             buttonImageOnly: false,
             changeMonth: true,
             changeYear: true,
             dateFormat: 'yy-mm-dd',
             onSelect: function () {
                 $(this).select();
             }
		    });		    		 
	});
</script>
<img id="top" src="top.png" alt="">
<div id="form_container">


	<form id="form_889069" class="appnitro" method="post" action="">
		<div class="form_description">
			<h2><?php echo $title; ?></h2>
		</div>
		<ul>
				<li id="li_2"><label class="description" id="team_label">Home Team</label>
				<div>
						<select id="home_team" name="home_team">
						<?php 
							if(isset($teams)){
			
								foreach($teams as $team){
									echo '<option value="'.$team->teamid.'">'.$team->team_name.'</option>"';
								}
							}
						?>										
						</select> 
				</div></li>
						<li id="li_2"><label class="description" id="team_label">Away Team</label>
				<div>
						<select id="away_team" name="away_team">
						<?php 
							if(isset($teams)){
			
								foreach($teams as $team){
									echo '<option value="'.$team->teamid.'">'.$team->team_name.'</option>"';
								}
							}
						?>										
						</select> 
				</div></li>
				<li id="li_2"><label class="description" id="startdate_label">Game Date</label>
				<div>
						<input id="start_date" name="start_date"
						class="element text small" type="text" maxlength="10" value="" />
				</div></li>
				<li id="li_2"><label class="description" id="location_label">Location</label>
				<div>
						<input id="location" name="location"
						class="element text small" type="text" maxlength="200" value="" />
				</div></li>
			
			<li class="buttons"><input type="hidden" id="game_id"
				name="game_id" value="" />
				<input type="hidden" id="tournament_id"
				name="tournament_id" value="" /> <input type="button" value="Save"
				id="save" class="button_text" /> <input type="button" value="Back"
				id="back" class="button_text" /></li>
		</ul>
	</form>
	
</div>
